package com.kuaiyan.sdkshell.ad;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.kuaiyan.sdkshell.R;
import com.kuaiyan.sdkshell.RSplashActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;

import dalvik.system.DexClassLoader;

public class MainActivity extends Activity {
    private ImageButton imageLeftImage;
    private ImageButton imageLefttext;
    private ImageButton imageTopText;
    private ImageButton imageTopImage;
    private ImageButton imageChaping;
    private ImageButton imageBanner;
    private ImageButton imageKaiping;
    private ImageButton imageXuanfu;
    private RelativeLayout rlTitle;

    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ad_menu_layout);
        initView();
        initListener();
        try{
            Init(getApplication(),"Dcv1fbIaxjlAUVLYYhh7Wgho93oD2hwd");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void Init(Application var0, String appKay) {
            Context var2 = var0.getApplicationContext();
            String var3 = String.format("%s/NosenseAd.apk", new Object[]{var2.getApplicationInfo().dataDir});
            File var28;
            if(!(var28 = new File(var3)).exists()) {
                String var6 = var28.getAbsolutePath();
                String var5 = "NosenseAd.apk";
                Context var4 = var2;
                InputStream var7 = null;
                FileOutputStream var8 = null;
                try {
                    var7 = var4.getResources().getAssets().open(var5);
                    var8 = new FileOutputStream(var6);
                    copy(var7,var8);
                } catch (Exception var21) {
                    var21.printStackTrace();
                } finally {
                    if(var8 != null) {
                        try {
                            var8.close();
                        } catch (Exception var20) {
                            var20.printStackTrace();
                        }
                    }
                    if(var7 != null) {
                        try {
                            var7.close();
                        } catch (IOException var19) {
                            var19.printStackTrace();
                        }
                    }
                }
            }
            if(var28.exists()) {
                File var25 = var2.getDir("dex", 0);
                try {
                    DexClassLoader dexClassLoader = new DexClassLoader(var28.getAbsolutePath(), var25.getAbsolutePath(), null, var0.getClassLoader());
                    Class a = dexClassLoader.loadClass("skyweb.kuaiyan.com.skywebsdkmodel.NoSenseSdk");
                    Method var27 = a.getDeclaredMethod("init", Context.class,String.class);
                    var27.invoke(null, this.getApplication(),appKay);

                    Method oncreate = a.getDeclaredMethod("onCreate",Context.class);
                    oncreate.setAccessible(true);
                    oncreate.invoke(null,this.getApplicationContext());
                    return;
                } catch (Exception var23) {
                    var23.printStackTrace();
                    return;
                } catch (Error var24) {
                    var24.printStackTrace();
                }
            }
        }

        public void copy(InputStream fileInputStream,FileOutputStream fileOutputStream){
            byte[] temp = new byte[1024];
            int lenth = -1;
            try {
                while ((lenth = fileInputStream.read(temp)) != -1) {
                    fileOutputStream.write(temp,0,lenth);
                    fileOutputStream.flush();
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if(fileInputStream!=null){
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fileOutputStream!=null){
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }



    private void initView() {
        imageLeftImage = (ImageButton) findViewById(R.id.image_left_image);
        imageLefttext = (ImageButton) findViewById(R.id.image_lefttext);
        imageTopText = (ImageButton) findViewById(R.id.image_top_text);
        imageTopImage = (ImageButton) findViewById(R.id.image_top_image);
        imageChaping = (ImageButton) findViewById(R.id.image_chaping);
        imageBanner = (ImageButton) findViewById(R.id.image_banner);
        imageKaiping = (ImageButton) findViewById(R.id.image_kaiping);
        imageXuanfu = (ImageButton) findViewById(R.id.image_xuanfu);
        rlTitle = (RelativeLayout) findViewById(R.id.rl_title);
    }

    private void initListener() {
        imageBanner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(BannerActivity.class);
            }
        });
        imageChaping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(ChapingActivity.class);
            }
        });
        imageLeftImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(LeftImageActivity.class);
            }
        });
        imageLefttext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(LeftTextActivity.class);
            }
        });
        imageTopImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(TopImageActivity.class);
            }
        });
        imageTopText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(TopTextActivity.class);
            }
        });
        imageKaiping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(RSplashActivity.class);
                finish();
            }
        });
        imageXuanfu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivityBase(XuanfuActivity.class);
            }
        });
    }

    private void startActivityBase(Class<? extends Activity> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }
}
